package com.DataFactory.TaskManagement.service.impl;

import com.DataFactory.TaskManagement.common.base.IBaseEnum;
import com.DataFactory.TaskManagement.common.enums.TaskTypeEnum;
import com.DataFactory.TaskManagement.common.exception.TaskArrangementException;
import com.DataFactory.TaskManagement.converter.TaskConverter;
import com.DataFactory.TaskManagement.mapper.TaskArrangementMapper;
import com.DataFactory.TaskManagement.mapper.TaskMapper;
import com.DataFactory.TaskManagement.model.bo.TaskBO;
import com.DataFactory.TaskManagement.model.entity.Task;
import com.DataFactory.TaskManagement.model.entity.TaskArrangement;
import com.DataFactory.TaskManagement.model.form.TaskForm;
import com.DataFactory.TaskManagement.model.query.TaskQuery;
import com.DataFactory.TaskManagement.model.vo.TaskPageVO;
import com.DataFactory.TaskManagement.service.TaskService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 40434
* @description 针对表【task】的数据库操作Service实现
* @createDate 2024-03-28 21:04:52
*/
@Service
@RequiredArgsConstructor
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService{

    private final TaskMapper taskMapper;

    private final TaskConverter converter;

    private final TaskArrangementMapper taskArrangementMapper;

    /**
     * 获取任务列表
     * @param queryParams
     * @return
     */
    @Override
    public Page<TaskPageVO> getTaskPage(TaskQuery queryParams) {
        Page<TaskBO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
        Page<TaskBO> boPage =  taskMapper.getTaskPage(page, queryParams);
        Page<TaskPageVO> result= converter.boPage2Pagevo(boPage);
        return result;
    }

    /**
     * 保存任务
     * @param form
     * @return
     */
    @Override
    public boolean saveTask(TaskForm form) {
        Task task = converter.form2entity(form);
        int taskId = taskMapper.insert(task);//返回新增id
        List<Integer> typeList = Arrays.asList(form.getType().split(",")).stream().map(Integer::parseInt).collect(Collectors.toList());
        for(Integer value : typeList){
            //保存子任务
            TaskArrangement arrangement = new TaskArrangement();
            arrangement.setTaskId(taskId);
            arrangement.setType(IBaseEnum.getLabelByValue(value, TaskTypeEnum.class));
            arrangement.setProcess("未开始");
            arrangement.setProcessRate("0%");
            arrangement.setName("未命名任务");
            int insert = taskArrangementMapper.insert(arrangement);
            if(insert <= 0) throw new TaskArrangementException("保存任务失败");
        }
        return taskId > 0;
    }

    /**
     * 更新任务
     * @param taskId
     * @param form
     * @return
     */
    @Override
    public boolean updateTask(Integer taskId, TaskForm form) {
        Task task = converter.form2entity(form);
        task.setId(taskId);
        boolean result = this.updateById(task);
        return result;
    }

    /**
     * 删除任务
     * @param ids
     * @return
     */
    @Override
    public boolean deleteTasks(String ids) {
        List<Integer> idList = Arrays.asList(ids.split(",")).stream().map(Integer::parseInt).collect(Collectors.toList());
        boolean result = this.removeByIds(idList);
        return result;
    }

    /**
     * 获取任务表单
     * @param taskId
     * @return
     */
    @Override
    public TaskForm getTaskForm(Integer taskId) {
        TaskBO taskBO= taskMapper.getTaskForm(taskId);
        TaskForm taskForm = converter.bo2from(taskBO);
        return taskForm;
    }

}
